<?php


namespace api\app\controller;




use cmf\controller\RestBaseController;
use Qiniu\Auth;
use Qiniu\Http\Error;
use Qiniu\Storage\UploadManager;
use think\facade\Filesystem;
use think\Request;

class UploadController extends RestBaseController
{

    //上传文件(本地)
    public function UploadImage()
    {
        $image = request()->file('image');
        $savename = Filesystem::disk('public')->putFile( 'user', $image);
        if ($savename){
            $this->success('上传成功',[
                'data' => $savename
            ]);
        }else{
            $this->error('上传失败');
        }
    }
    //上传七牛云
    public function qiniu()
    {
        $file = request()->file('image');
        if (!$file) {
            $this->error('请选择文件');
        }
        // 用于签名的公钥和私钥
        $accessKey = config('app.qiNiu.access_Key');
        $secretKey = config('app.qiNiu.secret_Key');
        //空间名
        $bucket = config('app.qiNiu.bucket_Name');
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);
        // 生成上传Token
        $token = $auth->uploadToken($bucket);
        // 要上传文件的本地路径
        $filePath = $file->getPathname();
        //获取后缀
        $type = $file->getOriginalExtension();
        // 上传到存储后保存的文件名
        $key = time().'.'.$type;
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        list($ret, $error) = $uploadMgr->putFile($token, $key, $filePath);
        if ($error !== null) {
            //$this->error('上传失败');
            //具体错误
            halt($error->message());
        }else{
           // halt($ret);
            $this->success('上传成功',[
                'key' => 'http://sel322gk3.hb-bkt.clouddn.com/'.$ret['key']
            ]);
        }



    }

}